package com.dylon.guiTest;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class Client {

    public static String input = "C:\\Users\\gefan\\Desktop\\worknote\\java-gui\\GIF2Char\\tiaowu.gif"; //gif图片路径，推荐使用像素在400*400以内的
    public static String output = "C:\\Users\\gefan\\Desktop\\worknote\\java-gui\\GIF2Char\\chargif\\tiaowu\\"; //gif图片生成路径，这里没有整合AnimatedGifEncoder包

    //所以生成的是一些jpg图片，需要自己借助其他工具整个成gif
    //如果有条件可以继续改进，直接生成一个gif文件
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                MainFrame mainFrame = new MainFrame();
                mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                mainFrame.setVisible(true);
            }
        });
    }
}

class MainFrame extends JFrame {
    private int x;
    private int y;
    public static int WIDTH = 0;
    public static int HEIGHT = 0;

    {
        Toolkit kit = Toolkit.getDefaultToolkit();
        int screen_width = kit.getScreenSize().width;
        int screen_height = kit.getScreenSize().height;
        x = (screen_width - WIDTH) / 2;
        y = (screen_height - HEIGHT) / 2;
    }

    public MainFrame() {
        setTitle("CharacterGIF_1.0");
        initBounds();
        createGIF();
        setBounds(x, y, WIDTH, HEIGHT);
        setResizable(false);
        setIconImage(null);

        JPanel panel = new GamePanel();
        Container container = getContentPane();
        container.add(panel);
    }

    //将字符图片输出到指定目录
    public void createGIF() {
        BufferedImage[] charImgs = ImgToCharacter.getCharImgs();
        try {
            File filePath = new File(Client.output);
            if (!filePath.exists()) {
                filePath.mkdirs();
            }
            for (int i = 0; i < charImgs.length; i++) {
                File file = new File(Client.output + i + ".jpg");
                ImageIO.write(charImgs[i], "jpg", file);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("图片输出完成！");
    }

    public void initBounds() {
        ImgToCharacter.readGiF();
        ImgToCharacter.draw();
        BufferedImage[] charImgs = ImgToCharacter.getCharImgs();
        int max_width = 0;
        int max_height = 0;
        for (BufferedImage img : charImgs) {
            if (img.getWidth() > max_width)
                max_width = img.getWidth();
            if (img.getHeight() > max_height)
                max_height = img.getHeight();
        }
        WIDTH = max_width;
        HEIGHT = max_height;
    }
}
